*********************************************************************************************************************
************************************ Multilevel Scatterplots: Nationalist Attitudes *********************************
****************************************  Random intercepts vs. Chinashock ******************************************
*********************************************************************************************************************
	clear 
	use "BHPS_merged.dta"
	
	sort pid waven
	
	* global with control variables
	global controls "i.education i.male c.age_cent##c.age_cent i.bornelsewhere2 i.parentsbornelsewhere"


*** Run multilevel models without level 2 predictors 
	mixed D3_nationalism L3.nationalism ///
		$controls i.ID_NUTS1xYear  ///
		if (year==2008 | year==2005 | year==2002) ///
		|| ID_NUTS3xYear: , stddeviations 			
						
		predict reffect_nat1, reffects
		predict reerrors_nat1, reses

								
	mixed D_EUsupport c.LDV_EUsupport##i.year ///
		$controls i.ID_NUTS1xYear  ///
		if (year==2006 | year==2002) ///
		|| ID_NUTS3xYear: , stddeviations 	
					
		predict reffect_eu1, reffects
		predict reerrors_eu1, reses

				
*** collapse to NUTS3xyear-structure
	preserve
	collapse (mean) reffect_nat* reffect_eu* reerrors_nat* reerrors_eu* ///
		real_ADH_imp_CH_d3* real_gr_imp_CH_d3* real_ADH_imp_CH_d_eu* real_gr_imp_CH_d_eu* year ///
		(last) nuts3_2006_name ///
		(count) D3_nationalism_n=D3_nationalism D_EUsupport_n=D_EUsupport ///
		, by (ID_NUTS3xYear)

	global ytitle "Estimated NUTS 3 region-year intercept"
				

*** Scatterplots with error bars
	set scheme plotplainblind
				
	* EU membership support
	pwcorr reffect_eu1 real_gr_imp_CH_d_eu_nl real_ADH_imp_CH_d_eu_nl, sig
												
	serrbar reffect_eu1 reerrors_eu1 real_gr_imp_CH_d_eu_nl, lcolor(gs10) lwidth(vthin) mvopts(msymbol(oh)) ///
		addplot(lfit reffect_eu1 real_gr_imp_CH_d_eu_nl, lcolor(black) lpattern(solid)) ///
		legend(off) ///
		ytitle($ytitle) ///
		xtitle("Growth rate of real imports from China (log)") ///
		text(-0.095 1.6 "r=-0.12 (p=0.07)") ///
		name(eu_gr, replace) nodraw
					
	serrbar reffect_eu1 reerrors_eu1 real_ADH_imp_CH_d_eu_nl, lcolor(gs10) lwidth(vthin) mvopts(msymbol(oh)) ///
		addplot(lfit reffect_eu1 real_ADH_imp_CH_d_eu_nl, lcolor(black) lpattern(solid)) ///
		legend(off) ///
		ytitle($ytitle) ///
		xtitle("Increase in real imports from China per worker (log)") ///
		text(-0.095 3.75 "r=-0.12 (p=0.07)") ///
		name(eu_ADH, replace) nodraw
								
	graph combine eu_gr eu_ADH , title(Support for EU membership) name(eu, replace) nodraw			
	
	* nationalist attitude
	pwcorr reffect_nat1 real_gr_imp_CH_d3_nl real_ADH_imp_CH_d3_nl, sig obs
													
	serrbar reffect_nat1 reerrors_nat1 real_gr_imp_CH_d3_nl, lcolor(gs10) lwidth(vthin) mvopts(msymbol(oh)) ///
		addplot(lfit reffect_nat1 real_gr_imp_CH_d3_nl, lcolor(black) lpattern(solid)) ///
		legend(off) ///
		ytitle($ytitle) ///
		xtitle("Growth rate of real imports from China (log)") ///
		text(-0.095 1 "r=0.12 (p=0.03)") ///
		name(nat_gr, replace) nodraw					
				
	serrbar reffect_nat1 reerrors_nat1 real_ADH_imp_CH_d3_nl, lcolor(gs10) lwidth(vthin) mvopts(msymbol(oh)) ///
		addplot(lfit reffect_nat1 real_ADH_imp_CH_d3_nl, lcolor(black) lpattern(solid)) ///
		legend(off) ///
		ytitle($ytitle) ///
		xtitle("Increase in real imports from China per worker (log)") ///
		text(-0.095 3.00 "r=0.10 (p=0.06)") ///
		name(nat_ADH, replace) nodraw
					
	graph combine nat_gr nat_ADH , title(Nationalist attitude) name(nat, replace) nodraw	

	* combine to one graph
	graph combine eu nat, cols(1) ysize(7) imargin(tiny) iscale(*.85) name(MLscatterplots, replace)					

	restore	